package com.Final.hj;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

public class LonginSQLOpa {
    //出生年数据库的实例
    private MySQLiteData helper;
    private Context mContext;
    SQLSavaInfo info;
    //创建 操作数据库的对象
    public LonginSQLOpa(Context context) {
        helper = new MySQLiteData(context);
        mContext = context;
    }

    //数据的插入
    public void insert(SQLSavaInfo info) {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("myId", info.getMyId());
        values.put("uname", info.getUname());
        values.put("paw", info.getPaw());
        values.put("email", info.getEmail());
        values.put("hImgId", info.gethImgId());
        values.put("type", info.getType());
        values.put("gender", info.getGender());
        db.insert("User", null, values);
        db.close();
    }

    //数据的更新  只能更新一条  只能根据myId进行修改
    public void update(String changeFlag, String myId, String data) {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();

        //用户名
        if(changeFlag.equals("uname")) {
            values.put("uname", data);
        }else if(changeFlag.equals("paw")){
            values.put("paw", data);
        }else{
            values.put("email", data);
        }

        db.update("User", values, "myId=?",
                new String[] { myId });
        db.close();
    }

    //查询 全部数据
    public SQLSavaInfo queryAll() {
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor c = db.query("User", new String[] { "myId", "paw", "uname", "email",  "hImgId", "type", "gender"}, null, null, null, null, null);

        //创建集合

        while (c.moveToNext()) {
            String uname = c.getString(c.getColumnIndex("uname"));
            String paw = c.getString(c.getColumnIndex("paw"));
            String hImgId = c.getString(c.getColumnIndex("hImgId"));
            String gender = c.getString(c.getColumnIndex("gender"));
            String type = c.getString(c.getColumnIndex("type"));
            String email = c.getString(c.getColumnIndex("email"));
            String myId = c.getString(c.getColumnIndex("myId"));

            //直接在类 User设置相应的值 之后可以调用 getxxx获取
            info = new SQLSavaInfo(uname,paw, myId, email, hImgId, type, gender);

        }
        c.close();
        db.close();
        return info;
    }

    //查询某条数据
    public String findData(String myId, String data) {
        //这里不应该是phone 因为 也可能是密码
        String queryData = "";
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor c = db.query("User", null, "myId=?", new String[] { myId },
                null, null, null);
        //将指针移到下一位 直到找到 data即可 data即为传过来的具体值
        while (c.moveToNext()) {
            queryData = c.getString(c.getColumnIndex(data));
        }

        Toast.makeText(mContext, queryData, Toast.LENGTH_SHORT).show();
        c.close();
        db.close();
        //返回数据
        return queryData;
    }

    //删除全部数据
    public void deleteAll() {
        SQLiteDatabase db = helper.getWritableDatabase();
        String sql = "DELETE FROM User;";
        db.execSQL(sql);
        db.close();
    }


}
